﻿Imports System.Linq
Imports System.Text
Imports System.Windows.Data

Public Class EventCountdownConverter
    Implements IValueConverter

#Region "IValueConverter Members"

    Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.Convert

        Dim timeLeft As TimeSpan = DirectCast(value, TimeSpan)

        If timeLeft = TimeSpan.MinValue Then
            Return "Sometime soon..."
        ElseIf timeLeft.Seconds < 0 Then
            Return "I should be back by now!"
        Else
            Dim msgBuffer As New StringBuilder()
            If timeLeft.Days > 0 Then
                msgBuffer.Append(timeLeft.Days)
                msgBuffer.Append(" day(s)")
            End If
            If timeLeft.Hours > 0 Then
                If msgBuffer.Length > 0 Then
                    msgBuffer.Append(", ")
                End If

                msgBuffer.Append(timeLeft.Hours)
                msgBuffer.Append(" hours(s)")
            End If
            If timeLeft.Minutes > 0 Then
                If msgBuffer.Length > 0 Then
                    msgBuffer.Append(", ")
                End If

                msgBuffer.Append(timeLeft.Minutes)
                msgBuffer.Append(" minutes(s)")
            End If
            If timeLeft.Seconds > 0 Then
                If msgBuffer.Length > 0 Then
                    msgBuffer.Append(", ")
                End If

                msgBuffer.Append(timeLeft.Seconds)
                msgBuffer.Append(" second(s)")
            End If

            Return msgBuffer.ToString()
        End If
    End Function

    Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
        Throw New NotImplementedException()
    End Function
#End Region
End Class
